package com.marmot.cloud.framework;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.stereotype.Indexed;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@SpringBootApplication
@EnableJpaRepositories
@EnableJpaAuditing
@EnableMongoAuditing
@Indexed
public class MarmotFrameworkAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(MarmotFrameworkAdminApplication.class, args);
    }

    @Bean
    public CommandLineRunner printMappings(ApplicationContext context) {
        return args -> {
            // 打印所有的 Request Mappings
            RequestMappingHandlerMapping mapping = context.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
            mapping.getHandlerMethods().forEach((key, value) -> System.out.println(key + " : " + value));

            // 打印所有的业务 Beans
            System.out.println("Printing all business beans:");
            String[] allBeanNames = context.getBeanDefinitionNames();
            for (String beanName : allBeanNames) {
                Object bean = context.getBean(beanName);
                if (bean.getClass().isAnnotationPresent(Service.class)) {
                    System.out.println(beanName + " : " + bean.getClass().getName());
                }
            }
        };
    }
}
